﻿using System;
using System.Text;
using System.Net.Mail;

namespace Hxsoft.AppV1
{
	public class SystemMail
	{
		// 以下配置将改为从配置文件读取。
		private static readonly string SmtpServer = "mail.sinoinfo.com.cn";
		private static readonly int? SmtpPort = 25;
		private static readonly string FromAddress = "System@sinoinfo.com.cn";
		private static readonly string FromName = "Sinoinfo";
		private static readonly string MailServerUserName = "System@sinoinfo.com.cn";
		private static readonly string MailServerPassWord = "123456";

		public SystemMail()
		{
		}

		public SystemMail(string subject, string recipients, string body, bool isBodyHtml)
		{
			this.Subject = subject;
			this.Recipients = recipients;
			this.Body = body;
			this.IsBodyHtml = isBodyHtml;
		}

		#region Property
		private string _subject;
		private string _body;
		private bool _isBodyHtml;
		private MailAddressCollection _to;

		/// <summary>
		/// 邮件主题。
		/// </summary>
		public string Subject
		{
			get { return _subject; }
			set { _subject = value; }
		}

		/// <summary>
		/// 邮件正文。
		/// </summary>
		public string Body
		{
			get { return _body; }
			set { _body = value; }
		}

		public MailPriority? Priority { get; set; }

		/// <summary>
		/// 获取包含此电子邮件的收件人的地址集合。
		/// </summary>
		public MailAddressCollection To
		{
			get
			{
				if (_to == null)
					_to = new MailAddressCollection();
				return _to;
			}
		}

		/// <summary>
		/// 收件人列表。
		/// </summary>
		public string Recipients
		{
			get
			{
				string recipients = null;

				foreach (MailAddress address in To)
				{
					if (string.IsNullOrEmpty(recipients))
						recipients = address.Address;
					else
						recipients += ";" + address.Address;
				}

				return recipients;
			}
			set
			{
				To.Clear();

				if (string.IsNullOrEmpty(value)) return;
				string[] recipients = value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
				foreach (string recipient in recipients)
				{
					To.Add(new MailAddress(recipient));
				}
			}
		}
		#endregion

		/// <summary>
		/// 获取或设置指示邮件正文是否为 Html 格式的值。
		/// </summary>
		public bool IsBodyHtml
		{
			get { return _isBodyHtml; }
			set { _isBodyHtml = value; }
		}

		/// <summary>
		/// 发送邮件。
		/// </summary>
		public void Send()
		{
			MailMessage mail = new MailMessage();
			mail.From = new MailAddress(FromAddress, FromName, Encoding.UTF8);

			foreach (MailAddress address in To)
			{
				mail.To.Add(address);
			}

			mail.Subject = Subject;
			mail.IsBodyHtml = IsBodyHtml;
			mail.Body = Body;
			mail.Priority = Priority.HasValue ? Priority.Value : MailPriority.Normal;
			mail.BodyEncoding = Encoding.UTF8;

			SmtpClient smtp = new SmtpClient();
			smtp.Host = SmtpServer;
			if(SmtpPort.HasValue) smtp.Port = SmtpPort.Value;
			smtp.Credentials = new System.Net.NetworkCredential(MailServerUserName, MailServerPassWord);

			//当不是25端口(gmail:587)
			if (SmtpPort != 25)
			{
				smtp.EnableSsl = true;
			}
			
			smtp.Send(mail);
		}

		/// <summary>
		/// 发送邮件。
		/// </summary>
		/// <param name="subject">包含邮件主题行的 String。</param>
		/// <param name="recipients">包含邮件收件人的地址的 String。</param>
		/// <param name="isBodyHtml">指示邮件正文是否为 Html 格式的值。</param>
		/// <param name="body">包含邮件正文的 String。</param>
		public static void Send(string subject, string recipients, string body, bool isBodyHtml)
		{
			SystemMail mail = new SystemMail();

			mail.Subject = subject;
			mail.Recipients = recipients;
			mail.Body = body;
			mail.IsBodyHtml = isBodyHtml;

			mail.Send();
		}
	}
}
